/* Copyright (C) 2009 Mobile Sorcery AB

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/

#include "mainScreen.h"
#include "pokerGame.h"
#include "memMatch.h"
#include "Util.h"
#include "userInfo.h"
#include "User.h"
#include <conprint.h>
#include <MAUI/Engine.h>
#include <MAUI/Font.h>
#include <MAUI/Label.h>
#include <MAUI/ListBox.h>
#include <MAUI/Image.h>
#include <MAUI/EditBox.h>
#include <MAUtil/Moblet.h>
#include <ma.h>
#include <mavsprintf.h>
#include <IX_WIDGET.h>
#include "highScores.h"

using namespace MAUtil;
using namespace MAUI;

MyMoblet *moblet;

MainScreen::MainScreen() {
	myUser->setName("AAA");
	screens.add(new pokerGame(this, myUser));
	screens.add(new memMatch(this, myUser));
	screens.add(new UserScreen(this, myUser));
	screens.add(new highScoreScreen(this));

	layout = createMainLayout("Select", "Exit");

//	bg = new Image(0, 0, scrWidth, scrHeight, 0, true, true, ARCADE);
//	bg->add(layout);

	mBackgroundArea = new Label(0,0,0,0,NULL);
	bg = new Image(0, 0, 200, 200, mBackgroundArea, true, true, ARCADE);
	bg->add(layout);

	listBox = (ListBox*) layout->getChildren()[0];

	listBox->add(createLabel("Play Poker"));
	listBox->add(createLabel("Play MemMatch"));
	listBox->add(createLabel("Set Player Info"));
	listBox->add(createLabel("View High Scores"));

	softKeys = layout->getChildren()[1];

	this->setMain(mBackgroundArea);

}

MainScreen::~MainScreen() {
	delete layout;
	for(int i = 0; i < screens.size(); i++) delete screens[i];
}

void MainScreen::keyPressEvent(int keyCode, int nativeCode) {
	switch(keyCode) {
	case MAK_UP:
		listBox->selectPreviousItem();
		break;
	case MAK_DOWN:
		listBox->selectNextItem();
		break;
	case MAK_FIRE:
	case MAK_SOFTLEFT:
		{
			int index = listBox->getSelectedIndex();
			if(index == screens.size()+1) {
				moblet->closeEvent();
				moblet->close();
			}
			else
				screens[index]->show();
		}
		break;
	case MAK_SOFTRIGHT:
		moblet->closeEvent();
		moblet->close();
		break;
	}
}

void MainScreen::pointerPressEvent(MAPoint2d point) {
	Point p;
	p.set(point.x, point.y);
	if(listBox->contains(p)) {
		for(int i = 0; i < listBox->getChildren().size(); i++) {
			if(listBox->getChildren()[i]->contains(p)) {
				int index = listBox->getSelectedIndex();
				if(index == i) {
					keyPressEvent(MAK_FIRE, 0);
				}
				else {
					listBox->setSelectedIndex(i);
				}
				break;
			}
		}
	}
	else if(softKeys->contains(p)) {
		if(softKeys->getChildren()[0]->contains(p)) {
			keyPressEvent(MAK_SOFTLEFT, 0);
		}
		else if(softKeys->getChildren()[1]->contains(p)) {
			keyPressEvent(MAK_SOFTRIGHT, 0);
		}
	}
}

void MainScreen::pointerReleaseEvent(MAPoint2d point) {
}

void MyMoblet::keyPressEvent(int keyCode, int nativeCode) {
}

void MyMoblet::keyReleaseEvent(int keyCode, int nativeCode) {
}

void MyMoblet::closeEvent() {
	// do destruction here
	delete mainScreen;
}

MyMoblet::MyMoblet() {
	gFont = new MAUI::Font(RES_FONT);
	gSkin = new WidgetSkin(RES_SELECTED, RES_UNSELECTED, 16, 32, 16, 32, true, true);
	bgSkin = new WidgetSkin(ARCADE, ARCADE, 16, 32, 16, 32, true, true);
	Engine& engine = Engine::getSingleton();
	engine.setDefaultFont(gFont);
	engine.setDefaultSkin(gSkin);

	MAExtent screenSize = maGetScrSize();
	scrWidth = EXTENT_X(screenSize);
	scrHeight = EXTENT_Y(screenSize);
	mainScreen = new MainScreen();
	mainScreen->show();

}

extern "C" int MAMain() {
	moblet = new MyMoblet();
	MyMoblet::run(moblet);
	return 0;
}
